<template>
  <BasicDrawer v-bind="$attrs" @register="registerDrawer" title="登录模式" :width="width" @ok="handleSubmit" destroyOnClose>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <a-button preIcon="ant-design:plus-outlined" type="primary" @click="handleAdd">新增</a-button>
      </template>
      <template #action="{ record }">
        <TableAction :actions="getActions(record)" />
      </template>
    </BasicTable>
    <LdpConnectDrawer @register="registerLdpConnectDrawer" @success="reload"/>
    <!-- <LdpFieldListModal @register="registerLdpFieldListModal"/> -->
  </BasicDrawer>
</template>
<script lang="ts" setup>
import { ref, useAttrs, onMounted, nextTick } from 'vue';
import MyAlert from '/@/components/My/MyAlert.vue';
import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
import { BasicTable, TableAction } from '/@/components/Table';
import { useModal } from '/@/components/Modal';
import { useDrawer } from '/@/components/Drawer';
import LdpConnectDrawer from './connect/LdpConnectDrawer.vue';
import { useGlobSetting } from '/@/hooks/setting';
import { columns } from './connect/data';
import { listLdpConnect, deleteLdpConnect } from './connect/api';
import { useListPage } from '/@/hooks/system/useListPage';
import { useMessage } from '/@/hooks/web/useMessage';
// 声明Emits
const emit = defineEmits(['success', 'register']);
const attrs = useAttrs()

const ldpId = ref('')
const width = ref<number | string>('45%')
// 列表页面公共参数、方法
const { prefixCls, tableContext } = useListPage({
  designScope: 'ldp-connect',
  tableProps: {
    title: '连接身份源',
    api: listLdpConnect,
    showTableSetting: false,
    showIndexColumn: false,
    bordered: false,
    striped: false,
    immediate: false,
    columns: columns,
    useSearchForm: false
  }
})

// const [registerLdpFieldListModal, { openModal: openFieldListModal  }] = useModal();
const [registerTable, { reload, setProps }] = tableContext;
const { createMessage } = useMessage();
const glob = useGlobSetting();
const [registerLdpConnectDrawer, { openDrawer: openLdpConnectDrawer }] = useDrawer();

const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
  setDrawerProps({ confirmLoading: false });
  ldpId.value = data.record.id
  setProps({searchInfo: { ldpId: data.record.id }});
  reload()
});



/**
 * 提交
 */
async function handleSubmit() {
  try {
    closeDrawer();
    emit('success');
  } finally {
    setDrawerProps({ confirmLoading: false });
  }
}

/**
* 新增事件
*/
function handleAdd() {
  openLdpConnectDrawer(true, { isUpdate: false, ldpId: ldpId.value });
}

/**
 * 删除事件
 */
async function handleDelete(record) {
  await deleteLdpConnect({ id: record.id }, reload);
}

const handleEdit = (record) => {
  openLdpConnectDrawer(true, { isUpdate: true, record, ldpId: ldpId.value });
}

/**
 * 操作列定义
 * @param record
 */
function getActions(record) {
  return [
    {
      label: '删除',
      ifShow: record.sendStatus != 1,
      popConfirm: {
        title: '是否确认删除',
        confirm: handleDelete.bind(null, record),
      },
    },
    {
      label: '编辑',
      onClick: handleEdit.bind(null, record)
    }
  ]
}
</script>

<style lang="less">

</style>
